

class DGame {
    public static _bottomLayer:fgui.GComponent;
    public static _sceneLayer:fgui.GComponent;
    public static _uiLayer:fgui.GComponent;
    public static _popupLayer:fgui.GComponent;
    public static _topLayer:fgui.GComponent;

    private static _uiPannels = {};
    private static _scene:DBaseScene;
    private static _sceneClass:any;

    private static _stageWidth:number;
    private static _stageHeight:number;


    public static init(main:egret.DisplayObjectContainer, design:DDesignResolution){
        this._bottomLayer = new fgui.GComponent();
        this._sceneLayer = new fgui.GComponent();
        this._uiLayer = new fgui.GComponent();
        this._popupLayer = new fgui.GComponent();
        this._topLayer = new fgui.GComponent();

        main.addChild(this._bottomLayer.displayObject);
        main.addChild(this._sceneLayer.displayObject);
        main.addChild(this._uiLayer.displayObject);
        main.addChild(this._popupLayer.displayObject);
        main.addChild(this._topLayer.displayObject);


        let stage = main.stage;
    
        let scaleMode = egret.StageScaleMode.SHOW_ALL;

        let maxW_dt = design.maxDesignWidth / design.designHeight;
        let maxH_dt = design.designWidth / design.maxDesignHeight;
        let design_dt = design.designWidth / design.designHeight;
        let stage_dt = stage.stageWidth / stage.stageHeight;
        if(stage_dt > design_dt){
            if(stage_dt <= maxW_dt){
                scaleMode = egret.StageScaleMode.FIXED_WIDTH;
            }
        }else if(stage_dt < design_dt){
            if(stage_dt >= maxH_dt){
                scaleMode = egret.StageScaleMode.FIXED_HEIGHT;
            }
        }else{
            scaleMode = egret.StageScaleMode.SHOW_ALL;
        }
        
        console.log("stage real resolution:", main.stage.stageWidth, main.stage.stageHeight);
        main.stage.scaleMode = scaleMode;
        
        this._stageWidth = main.stage.stageWidth;
        this._stageHeight = main.stage.stageHeight;
        console.log("stage resolution:", this.stageWidth, this.stageHeight, scaleMode);
    }

    public static get stageWidth():number{
        return this._stageWidth;
    }

    public static get stageHeight():number{
        return this._stageHeight;
    }

    public static showPannel(pannel:DBasePannel, key?:string){
        if(key){
            let exist:DBasePannel = this._uiPannels[key];
            if(exist){
                if(exist == pannel){ return; }
                exist.hide();
            }
        }
        pannel._KEY = key;
        this._uiLayer.addChild(pannel);
        this._uiPannels[key] = pannel;
        pannel.show();
        return;
    }

    
    public static showUIByPannel(pannel:DBasePannel):DBasePannel{
        this._uiLayer.addChild(pannel);
        pannel.preStart();
        return pannel;
    }

    public static hidePannel(pannel:DBasePannel){
        let key = pannel._KEY;
        pannel.destroy();
        if(key){
            delete this._uiPannels[key];
        }
        return pannel;
    }

    public static hideAllUI(){
        for (var k in this._uiPannels){
            let pannel:DBasePannel = this._uiPannels[k];
            pannel.destroy();
        }
        this._uiPannels = {};
    }

    public static onHidePannel(pannel:DBasePannel):DBasePannel{
        pannel.parent.removeChild(pannel);
        if(pannel._KEY){
            delete this._uiPannels[pannel._KEY];
        }
        return pannel;
    }

    public static changeScene(_scene:DBaseScene):DBaseScene{
        if(this._sceneClass == _scene){
            return this._scene;
        }
        this.hideAllUI();
        if(this._scene){
            this._scene.destroy();
        }
        this._scene = _scene;
        this._sceneClass = _scene;
        this._sceneLayer.addChild(this._scene);
        return this._scene;
    }

    public static createBasicView(resName:string):fgui.GObject{
        let paths = DConfig.basicPackage.split("/");
        let package_name = paths[paths.length - 1];
        return fgui.UIPackage.createObject(package_name, resName);
    }
    
    public static createView(package_:string, resName:string, userClass?:any):fgui.GObject{
        return fgui.UIPackage.createObject(DConfig.assetsPath + package_, resName, userClass);
    }
}

